package com.project.lk.nurseapp.common.service;

import android.app.Notification;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v7.app.NotificationCompat;
import android.util.Log;

import com.project.lk.nurseapp.R;
import com.project.lk.nurseapp.base.BaseApp;
import com.project.lk.nurseapp.db.RemindBean;
import com.project.lk.nurseapp.db.RemindBeanDao;

import java.util.List;

/**
 * 功能介绍：
 * 项目名：
 * 模块名称：
 * 作者：LK
 * 日期：2018/1/9
 * 修改日期：
 */
public class RemindService extends Service{
    static final int NOTIFICATION_ID = 1001;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        try{
            Log.d("nuts", "KeepAliveService onStartCommand invoke");
            RemindBeanDao remindBeanDao = BaseApp.getInstance().getDaoSession().getRemindBeanDao();
            List<RemindBean> remindBeans = remindBeanDao.loadAll();
            if (remindBeans!=null) {
                if (remindBeans.size()!=0) {
                    for (int i=0;i<remindBeans.size();i++){
                        String dateStr = remindBeans.get(i).getDateStr();  //TODO:显示提醒
                        startForeground(NOTIFICATION_ID, getNotification(this,dateStr));
                    }
                }
            }
            Intent innerIntent = new Intent(this, InnerService.class);
            startService(innerIntent);


        }catch (Exception e){
            //nothing to do
        }
        // 利用系统自启
        return START_STICKY;
    }

    public  static class InnerService extends Service {

        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }

        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {

            try{
                Log.d("nuts", "InnerService onStartCommand invoke");


//                if (JPushInterface.isPushStopped(this)) {
//                    JPushInterface.onResume(this);
//                }
                RemindBeanDao remindBeanDao = BaseApp.getInstance().getDaoSession().getRemindBeanDao();
                List<RemindBean> remindBeans = remindBeanDao.loadAll();
                if (remindBeans!=null) {
                    if (remindBeans.size()!=0) {
                        for (int i=0;i<remindBeans.size();i++){
                            String dateStr = remindBeans.get(i).getDateStr();  //TODO:显示提醒
                            startForeground(NOTIFICATION_ID, getNotification(this,dateStr));
                        }
                    }
                }
                stopSelf();
            }catch (Exception e){
                //nothing to do
            }
            return super.onStartCommand(intent, flags, startId);
        }
    }

    private static Notification getNotification(Context ctx,String content){

        Notification notify = new NotificationCompat.Builder(ctx)
                .setContentTitle(ctx.getResources().getString(R.string.app_name))
                .setContentText(content)
                .setAutoCancel(true)
                .build();
        return notify;
    }
}
